package model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.pojo.DongSanPham;
import model.util.DataHelper;
import controller.util.ControllerHelper;

public class DongSanPhamDAO 
{
	public static ArrayList<DongSanPham> DongSanPhamTheoNSX(int MaLoaiSP , int MaHangSX){
//	{
		DataHelper db = new DataHelper();
        if(!db.open())
            return null ;
        String sp = String.format(ControllerHelper.getStoreProcedureString("DongSanPhamTheoNSX"),MaLoaiSP,MaHangSX);
        ArrayList<DongSanPham> arr = null ;
        ResultSet rs = db.executeQuery(sp);
        try
        {
        	if(rs.next())
        	{
	            arr = new ArrayList<DongSanPham>();
	            do
	            {
	            	DongSanPham p = new DongSanPham();
	            	
	            	p.setMaDongSP(Integer.parseInt(rs.getString(1)));
	            	p.setMoTa(rs.getString(2));
	                
	            	arr.add(p);
	            }while(rs.next());
            }
        }
        catch(Exception ex){}
        db.close();
        return arr;
	}
	
	public static ArrayList<DongSanPham> DongSanPham(int MaLoaiSP){
//	{
		DataHelper db = new DataHelper();
        if(!db.open())
            return null ;
        String sp = String.format(ControllerHelper.getStoreProcedureString("DongSanPham"),MaLoaiSP);
        ArrayList<DongSanPham> arr = null ;
        ResultSet rs = db.executeQuery(sp);
        try
        {
        	if(rs.next())
        	{
	            arr = new ArrayList<DongSanPham>();
	            do
	            {
	            	DongSanPham p = new DongSanPham();
	            	
	            	p.setMaDongSP(Integer.parseInt(rs.getString(1)));
	            	p.setMoTa(rs.getString(2));
	                
	            	arr.add(p);
	            }while(rs.next());
            }
        }
        catch(Exception ex){}
        db.close();
        return arr;
	}
	
	public static ArrayList<ArrayList<String>> SanPhamTheoDanhMuc(int MaLoaiSP , int MaNSX , int MaDongSP){
//	{		
		DataHelper db = new DataHelper();
        if(!db.open())
            return null ;
        String sp = String.format(ControllerHelper.getStoreProcedureString("SanPhamTheoDanhMuc"),MaLoaiSP,MaNSX,MaDongSP);
        ArrayList<ArrayList<String>> arr = null ;
        ResultSet rs = db.executeQuery(sp);
		try 
		{
			if(rs.next())
			{
		        arr = new ArrayList<ArrayList<String>>();
		        do
		        {
		        	ArrayList<String> temp = new ArrayList<String>();
		        	
		        	temp.add(rs.getString("MaSP"));//MaSP
		        	temp.add(rs.getString("TenSP"));//TenSP
		        	temp.add(rs.getString("GiaBan"));//GiaBan
		        	temp.add(rs.getString("MoTaSP"));//MoTaSP
		        	temp.add(rs.getString("ThoiGianBaoHanh"));//ThoiGianBH

		            if(rs.getInt("SoLuongTon") >= 1)
		            	temp.add("Còn Hàng");//SoLuongTon
		            else
		            	temp.add("Hết Hàng");//SoLuongTon
		            
		            temp.add(rs.getString("SoLuotXem"));//SoLuotXem
		            temp.add(rs.getString("Link"));//HinhAnh
		            temp.add(rs.getString(9));//TenNSX hoac TenDongSP
		            temp.add(rs.getString("urlChiTiet"));
		            arr.add(temp);
		        }while(rs.next());
			}
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        db.close();
        return arr;
	}
	
	public static ArrayList<DongSanPham> LayDongSanPham(int op){
//		{
			DataHelper db = new DataHelper();
	        if(!db.open())
	            return null ;
	        String sp = String.format(ControllerHelper.getStoreProcedureString("DongSanPham1"),op);
	        ArrayList<DongSanPham> arr = null ;
	        ResultSet rs = db.executeQuery(sp);
	        try
	        {
	        	if(rs.next())
	        	{
		            arr = new ArrayList<DongSanPham>();
		            do
		            {
		            	DongSanPham p = new DongSanPham();
		            	
		            	p.setMaDongSP(Integer.parseInt(rs.getString(1)));
		            	p.setMoTa(rs.getString(2));
		                
		            	arr.add(p);
		            }while(rs.next());
	            }
	        }
	        catch(Exception ex){}
	        db.close();
	        return arr;
		}

	public static ArrayList<DongSanPham> DongSanPhamTheoHangSX(int MaHangSX){

		ArrayList<DongSanPham> _arr= new ArrayList<DongSanPham>();
		DataHelper helper = new DataHelper();
		
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(MaHangSX);
            String sp = ControllerHelper.getStoreProcedureString("DongSanPhamTheoHangSX");
            ResultSet result = helper.executeProcedure(sp, para);
            while(result.next())
            {
               
            	DongSanPham d = new DongSanPham();
            	d.setMaDongSP(result.getInt("MaDong"));
            	d.setMaHangSX(result.getInt("HangSanXuat"));
            	d.setMaLoaiSP(result.getInt("LoaiSP"));
            	d.setMoTa(result.getString("MoTa"));
                _arr.add(d);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		
		return _arr;
	}
	//thêm dòng sp
	public static boolean ThemDongSP(DongSanPham dsp)
	{
		boolean kq = false;
		DataHelper helper = new DataHelper();
		
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(dsp.getMoTa());
            para.add(dsp.getMaLoaiSP());
            para.add(dsp.getMaHangSX());
            String sp = ControllerHelper.getStoreProcedureString("ThemDongSP");
            int kqIns = helper.executeInsProcedure(sp, para);
            if(kqIns>0)
            {
            	kq=true;
            }            
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		return kq;
	}
	//kiem tra dong san pham theo ten
	public static ArrayList<DongSanPham> KiemTraDongSanPham(String tenDong)
		{
			ArrayList<DongSanPham> _arr= new ArrayList<DongSanPham>();
			DataHelper helper = new DataHelper();
			
	        try
	        {
	            helper.open();
	            ArrayList<Object> para = new ArrayList<Object>();
	            para.add(tenDong);
	            String sp = ControllerHelper.getStoreProcedureString("KiemTraDongSP");
	            ResultSet result = helper.executeProcedure(sp, para);
	            while(result.next())
	            {
	               
	            	DongSanPham d = new DongSanPham();
	            	d.setMaDongSP(result.getInt("MaDong"));
	            	d.setMaHangSX(result.getInt("HangSanXuat"));
	            	d.setMaLoaiSP(result.getInt("LoaiSP"));
	            	d.setMoTa(result.getString("MoTa"));
	                _arr.add(d);
	            }
	            
	        }catch(Exception ex)
	        {
	            System.out.println(ex.getMessage());
	        }
	        finally
	        {
	            helper.close();
	        }
			
			return _arr;
		}
}
